package org.fenixsoft.jvm.chapter8;

/**
 * 方法静态分派演示
 * @author zzm
 */
public class StaticDispatch {

    static abstract class Human {
    }

    static class Man extends Human {
    }

    static class Woman extends Human {
    }

    public void sayHello(Human guy) {
        System.out.println("hello,guy!");
    }

    public void sayHello(Man guy) {
        System.out.println("hello,gentleman!");
    }

    public void sayHello(Woman guy) {
        System.out.println("hello,lady!");
    }

    /**
     *     方法重载时，编译器是通过参数的静态类型作为判断依据，来决定调用方法的版本
     *     #13 = Methodref          #11.#43        // org/fenixsoft/jvm/chapter8/StaticDispatch.sayHello:(Lorg/fenixsoft/jvm/chapter8/StaticDispatch$Human;)V
     * @param args
     */
    public static void main(String[] args) {
        Human man = new Man();
        Human woman = new Woman();
        StaticDispatch sr = new StaticDispatch();
        sr.sayHello(man); // invokevirtual #13                 // Method sayHello:(Lorg/fenixsoft/jvm/chapter8/StaticDispatch$Human;)V
        sr.sayHello(woman); // invokevirtual #13                 // Method sayHello:(Lorg/fenixsoft/jvm/chapter8/StaticDispatch$Human;)V
    }
}

/**
 *
 * new man
 * new Dis
 * new Dis
 * new woman
 * new
 *
 */